package cn.zqh.user.config;

import cn.zqh.bean.api.CommonResult;
import cn.zqh.bean.api.ResultCode;
import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import feign.FeignException;
import feign.Response;
import feign.Util;
import feign.codec.DecodeException;
import feign.codec.Decoder;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.lang.reflect.Type;

/**
 * @Author: zqh
 * @Date: 2023-01-06-19:59
 * @Description: 把fegin远程调用的结果解析$
 */
public class CustomDecoderConfig implements Decoder {
    @Autowired
    public ObjectMapper objectMapper;


    @Override
    public Object decode(Response response, Type type) throws IOException, DecodeException, FeignException {
        String bodyStr = Util.toString(response.body().asReader(Util.UTF_8));
        CommonResult commonResult = JSON.parseObject(bodyStr, CommonResult.class);
        System.out.println(commonResult.getData());
        System.out.println("张启航狂空来流放到狂赛道");
        System.out.println(commonResult.getCode());

//        if (commonResult.getCode()==200) {
//            Object data = commonResult.getData();
//            JavaType javaType = TypeFactory.defaultInstance().constructType(type);
//            Object o = objectMapper.convertValue(data, javaType);
//
//            return o;
//        }
//        return CommonResult.failed(ResultCode.AnErrorOccurredInTheRemoteCall);
        //无需拿到其他东西，只要状态码
        return commonResult;
    }
}
